/*
 * @Author: zhangshu
 * @email: shiliang.zhang@tuya.com
 * @LastEditors: Please set LastEditors
 * @file name: app_key.c
 * @Description: 
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company: http://www.tuya.com
 * @Date: 2022-02-08
 * @LastEditTime: 2022-02-08
 */
#include "tal_memory.h"

#include "app_common.h"

#include "app_key.h"
#include "app_mfg.h"
#include "bsp.h"


//key push short times
#define KEY_PUSH_TIME_SHORT               700
#define KEY_1_IO_INDEX                    0
//key id



/**
 * @description: key init
 * @param[in] config: key config
 * @return: OPERATE_RET, success when OPRT_OK, or else
 */
OPERATE_RET app_key_oem_init(key_config_t *config)
{
    tal_key_gpio_init(config, 5, 50);
    TAL_PR_TRACE("key init OK!!!");
    return OPRT_OK;
}

/**
 * @description: key processing
 * @param [in] key_id: the key_id of key
 * @param [in] key_st: the trigger state of key
 * @param [in] push_time: press time of key
 * @return: none
 */
VOID_T gpio_key_interrupt_handler(UINT32_T key_id, KEY_ST_T key_st, UINT32_T push_time)
{
  UINT8_T  i;
  static UINT8_T flag;
  if (((KEY_1_IO_INDEX + 1) <= key_id)) {
      return;
  }
  switch (key_id) {
      case KEY_1_IO_INDEX:
      {
          if (key_st == KEY_ST_PUSH) {
              net_key_press_callback(push_time);
              TAL_PR_TRACE("(key_st == KEY_ST_PUSH) %d \r\n", push_time);
          } else {
              if (mfg_key_test_callback(key_id)) {
                  return;
              }
              if (push_time >= KEY_PUSH_TIME_SHORT) {
                  // net_key_long_press_release_callback(push_time);
                  // TAL_PR_TRACE("push_time >= KEY_PUSH_TIME_SHORT %d \r\n", push_time);
              }
              else
              {
                  // net_key_short_press_release_callback(0);
                  TAL_PR_TRACE("key_id %d,get_rel_status(key_id) %d  \r\n", key_id, get_rel_status(key_id));
                  TAL_PR_TRACE("device_status.relStatus %d,  \r\n", device_status.relStatus);
                  general_control = !general_control;
                  flag = !flag;
                  if (flag) {
                    for (i = 0; i < CHANNEL_NUM; i++) {
                        rel_on(i, 1);
                    }
                  } else {
                    for (i = 0; i < CHANNEL_NUM; i++) {
                        rel_off(i, 1);
                    }
                  }

    //                   device_status.led &= ~LED_STATUS_PRESS_SLOW;
    // tal_sw_timer_stop(etimer_network_up_delay_event_id);
                  TAL_PR_TRACE("push_time <= KEY_PUSH_TIME_SHORT %d \r\n", push_time);
              }
          }
          break;
      }
      // case KEY_2_IO_INDEX:
      // {
      //   break;
      // }
      // case KEY_3_IO_INDEX:
      // {
      //   break;
      // }
      default: {
          break;
      }
  }
}


